<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>



<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="hevea 1.08">
<LINK rel="stylesheet" type="text/css" href="umsroot.css">
<TITLE>
More about Priorities
</TITLE>
</HEAD>
<BODY >
<A HREF="umsroot113.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot115.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<HR>

<H2 CLASS="section"><A NAME="htoc253">17.10</A>&nbsp;&nbsp;More about Priorities</H2><UL>
<LI><A HREF="umsroot114.html#toc141">Changing Priority Explicitly</A>
<LI><A HREF="umsroot114.html#toc142">Choice of Priorities</A>
</UL>

For the scheduled goals,
ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> uses an execution model which is based on goal <I>priorities</I>
and which guarantees that a scheduled goal with a higher priority
will be always executed before any goal with lower priority.
Priority is a small integer number ranging from 1 to 12,
1 being the highest priority and 12 the lowest
(cf. figure&nbsp;<A HREF="umsroot106.html#figresolv">17.1</A>).
All goals started from the ECL<SUP><I>i</I></SUP>PS<SUP><I>e</I></SUP> top-level loop
or from the command line with the -e option have priority 12.
Each suspension and each goal which is being
executed therefore has an associated priority.
The priority of the currently executing goal can be determined
with <A HREF="../bips/kernel/suspensions/get_priority-1.html"><B>get_priority/1</B></A><A NAME="@default1034"></A>.<BR>
<BR>
Priority-based execution is driven by a scheduler:
It picks up the scheduled suspension with the highest priority.
If its priority is higher than the priority of the currently
executing goal, then the execution of the current goal
is interrupted and the new suspension is executed.
This is repeated until there are no suspensions
with priority higher than that of the current goal.<BR>
<BR>
<A NAME="toc141"></A>
<H3 CLASS="subsection"><A NAME="htoc254">17.10.1</A>&nbsp;&nbsp;Changing Priority Explicitly</H3>
It is also possible to execute a goal with a given priority
by means of
<A HREF="../bips/kernel/suspensions/call_priority-2.html"><B>call_priority(Goal, Prio)</B></A><A NAME="@default1035"></A>
which calls <B>Goal</B> with the priority <B>Prio</B>.
When a goal is called this way with high priority, it is effectively
made atomic, ie. it will not be interrupted by goals with lower priority
that wake up while it executes.
Those goals will all be deferred until exit from
<A HREF="../bips/kernel/suspensions/call_priority-2.html"><B>call_priority/2</B></A><A NAME="@default1036"></A>.
This technique can sometimes improve efficiency.
Consider for example the following program:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
p(1).
report(Term) :-
    writeln(term=Term),
    suspend(report(Term),3,Term-&gt;inst).
</PRE></BLOCKQUOTE>
and the execution
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 2]: report(f(X,Y,Z)), p(X),p(Y),p(Z).
term = f(X, Y, Z)
term = f(1, Y, Z)
term = f(1, 1, Z)
term = f(1, 1, 1)
</PRE></BLOCKQUOTE>
report/1 is woken and executed three times, once for each variable binding.
If instead we do the three bindings under high priority, it will only
execute once after all bindings have already been done:
<BLOCKQUOTE CLASS="quote"><PRE CLASS="verbatim">
[eclipse 3]: report(f(X,Y,Z)), call_priority((p(X),p(Y),p(Z)), 2).
term = f(X, Y, Z)
term = f(1, 1, 1)
</PRE></BLOCKQUOTE>
<A NAME="toc142"></A>
<H3 CLASS="subsection"><A NAME="htoc255">17.10.2</A>&nbsp;&nbsp;Choice of Priorities</H3>
Although the programmer is more or less free to specify
which priorities to use, we strongly recommend
to stick to the following scheme (from urgent to less urgent):
<DL CLASS="description" COMPACT=compact><DT CLASS="dt-description">
<B>debugging (1)</B><DD CLASS="dd-description"> goals which don't contribute to the semantics
of the program and always succeed, e.g. display routines, consistency
checks or data breakpoints.<BR>
<BR>
<DT CLASS="dt-description"><B>immediate</B><DD CLASS="dd-description"> goals which should be woken immediately
and which do not do any bindings or other updates.
Examples are quick tests which can immediately fail and
thus avoid redundant execution.<BR>
<BR>
<DT CLASS="dt-description"><B>quick</B><DD CLASS="dd-description"> fast deterministic goals which may
propagate changes to other variables.<BR>
<BR>
<DT CLASS="dt-description"><B>normal</B><DD CLASS="dd-description"> deterministic goals which should be woken
after the <B>quick</B> class.<BR>
<BR>
<DT CLASS="dt-description"><B>slow</B><DD CLASS="dd-description"> deterministic goals which require
a lot of processing, e.g. complicated disjunctive
constraints.<BR>
<BR>
<DT CLASS="dt-description"><B>delayed</B><DD CLASS="dd-description"> nondeterministic goals
or goals which are extremely slow.<BR>
<BR>
<DT CLASS="dt-description"><B>toplevel goal (12)</B><DD CLASS="dd-description"> the default priority of the user program.
</DL>
<HR>
<A HREF="umsroot113.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="umsroot104.html"><IMG SRC ="contents_motif.gif" ALT="Up"></A>
<A HREF="umsroot115.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
</BODY>
</HTML>
